package org.chameleon.datasource.factory;

import org.chameleon.datasource.cache.DefaultCacheFreshStore;
import org.chameleon.lang.factory.CacheFactory;

import java.util.Map;
import java.util.Objects;


/**
 * 缓存工厂抽象类
 * @param <T>
 * @param <P>
 */
public abstract class AbsCacheFactory <T,P> implements CacheFactory<T,P> {

    private final DefaultCacheFreshStore<P> defaultCacheFreshStore ;

    public AbsCacheFactory() {
        defaultCacheFreshStore = new DefaultCacheFreshStore<>();
    }
    @Override
    public P get(T t) {
        final String cacheKey = getCacheKey(t);
        P p = defaultCacheFreshStore.get(cacheKey);
        if (Objects.isNull(p)){
            p = createP(t);
            defaultCacheFreshStore.put(cacheKey,p);
        }
        return p;
    }
    @Override
    public void put(String key ,P p){
        defaultCacheFreshStore.put(key,p);
    }

    public abstract P createP(T t);


    public Map<String, P>  getAllProduct(){
        final Map<String, P> allCache = defaultCacheFreshStore.getAllCache();
        return allCache;
    }



}
